package org.suburban.data;
import java.util.LinkedList;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

import static org.suburban.data.Station.*;
import static org.suburban.resource.Strings.UNKNOWN_STATION_NAME;
/**
 * Class to u[load/delete station from repository
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 *
 */
public class StationRepository {

	/**
	 * save/update station data in the database
	 * @param id			- station id (key)
	 * @param name			- station human readable name
	 * @param comments		- station comments
	 * @param direction		- direction id
	 * @param index			- station index under direction
	 */
	public static void put(String id, String name, String comments, String direction, int index)
	{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		//Entity station = new Entity(TYPE, id);
		Entity station = new Entity(TYPE);
		station.setProperty(ID, id);
		station.setProperty(NAME, name);
		station.setProperty(LOWERCASE_NAME, name.toLowerCase());
		station.setProperty(COMMENTS, comments);
		station.setProperty(DIRECTION, direction);
		station.setProperty(INDEX, index);
		datastore.put(station);
	}
	
	/** 
	 * get station by name
	 * @param name 	- station human readable name
	 * @return		- list of stations
	 */
	public static Station[] getStationByName(String name)
	{
		LinkedList<Station> stations = new LinkedList<Station>();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query(TYPE);
		query.addFilter(LOWERCASE_NAME, Query.FilterOperator.EQUAL, name.toLowerCase());
		//query.addSort(DIRECTION);
		
		PreparedQuery prQuery = datastore.prepare(query);
		for(Entity entity : prQuery.asIterable())
		{
			stations.add(new Station(entity));
		}
		return stations.toArray(new Station[]{});
	}
	
	/**
	 * get station readable name by station id
	 * @param id	- station id
	 * @return	readable name
	 */
	public static String getName(String id)
	{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query(TYPE);
		query.addFilter(ID, Query.FilterOperator.EQUAL, id);
		
		PreparedQuery prQuery = datastore.prepare(query);
		for(Entity entity : prQuery.asIterable())
		{
			return new StringBuilder().append(entity.getProperty(NAME).toString()).append(" ").
			append(entity.getProperty(COMMENTS)).toString();
		}
		
		return UNKNOWN_STATION_NAME;
	}
	
	/**
	 * get station direction by station id
	 * @param id	- station id
	 * @return	direction
	 */
	public static String getDirection(String id)
	{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query(TYPE);
		query.addFilter(ID, Query.FilterOperator.EQUAL, id);
		
		PreparedQuery prQuery = datastore.prepare(query);
		for(Entity entity : prQuery.asIterable())
		{
			return entity.getProperty(DIRECTION).toString();
		}
		
		return UNKNOWN_STATION_NAME;
	}
}
